package com.huang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huang.mapper.RoleMapper;
import com.huang.mapper.UserMapper;
import com.huang.pojo.*;
import com.huang.utils.ToListInt;
import com.huang.vo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private MenuService menuService;

    public User isExistName(String name){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIsDelEqualTo(0);
        criteria.andU_loginNameEqualTo(name);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() == 0) return null;
        else return users.get(0);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    public int insert(User user) {
        user.setIsDel(0);
        return userMapper.insert(user);
    }

    /**
     * 分页+条件模糊查询
     * @param loginName
     * @param pageIndex
     * @param pageSize
     * @return 返回带有分页信息的对象
     */
    public PageResult list(String loginName, Integer pageIndex, Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<User> users = userMapper.selectByPageParam(loginName);
        PageInfo<User> info = new PageInfo<>(users);
        // 封装数据
        PageResult pageResult = new PageResult(users,
                (int) info.getTotal(),
                info.getPageNum(),
                info.getPages(),
                info.getPrePage(),
                info.getNextPage(),
                1,
                info.getPages(),
                info.getNavigatepageNums());
        return pageResult;
    }


    /**
     * 通过userId查找userInfo
     * @param id
     * @return
     */
    public User findById(Integer id){
        // 先查询role
        User user = userMapper.selectByPrimaryKey(id);
        // 通过userId 查询 role菜单
        Role role = roleMapper.selectByPrimaryKey(user.getR_id());
        // 根据roleId 查询
        user.setRole(role);
        return user;
    }

    /**
     * 通过主键更新信息
     * @param user
     * @return
     */
    public int update(User user){
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        User user = new User();
        user.setIsDel(1);
        user.setU_id(id);
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    /**
     * 逻辑批量删除
     * @param ids 字符串数组
     * @return
     */
    public int delete(String[] ids){
        // 将 String数组转换为 list<Integer>
        List<Integer> list = ToListInt.StringToList(ids);
        int i = userMapper.deleteByBatch(list);
        return i;
    }

}
